package com.xhd.file;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

import com.xhd.base.log.Log;
import com.xhd.base.log.LogFactory;
import com.xhd.exception.XFileException;
import com.xhd.server.ServerConfig;

public class XZipFile {
	private static final Log LogObj;
	static Class XZipFileClass;

	static {
		Class tmpclass = XZipFileClass;
		if (tmpclass == null) {

			try {
				XZipFile.XZipFileClass = Class
						.forName("com.sinocc.base.file.XZipFile");
			} catch (ClassNotFoundException localClassNotFoundException) {
				throw new NoClassDefFoundError(
						localClassNotFoundException.getMessage());
			}
		}
		LogObj = LogFactory.getLog(tmpclass);
	}

	public String getFirstPath(String paramString) throws XFileException {
		ZipFile localZipFile = null;
		File localFile = new File(paramString);
		if (!(localFile.exists()))
			throw new XFileException(60002, localZipFile.getName());
		try {
			localZipFile = new ZipFile(localFile);
			if ((localZipFile == null) || (localZipFile.size() < 1))
				throw new XFileException(60021);
			Enumeration localEnumeration = localZipFile.entries();
			ZipEntry localZipEntry = null;
			while (localEnumeration.hasMoreElements()) {
				localZipEntry = (ZipEntry) localEnumeration.nextElement();
				if (localZipEntry == null)
					continue;
				String str1 = localZipEntry.getName();
				int i = str1.indexOf(47);
				if (i > 0) {
					return str1.substring(0, i);

				}
				String str2 = str1;
				return str2;
			}
			return null;
		} catch (XFileException localXFileException) {
			throw localXFileException;
		} catch (Exception localException1) {
			LogObj.error(localException1);
			throw new XFileException(60021);
		} finally {
			try {
				localZipFile.close();
			} catch (Exception localException2) {
			}
		}
	}

	public OutputStream getZipOutput(String paramString1, String paramString2)
			throws XFileException {
		File localFile = new File(paramString1);
		if (!(localFile.exists()))
			try {
				if (!(localFile.createNewFile()))
					throw new XFileException(60016, localFile.getName());
			} catch (Exception localException1) {
				throw new XFileException(60016, localFile.getName());
			}
		if ((paramString2 == null) || (paramString2.length() < 1))
			paramString2 = localFile.getName();
		try {
			FileOutputStream localFileOutputStream = new FileOutputStream(
					localFile);
			ZipOutputStream localZipOutputStream = new ZipOutputStream(
					localFileOutputStream);
			ZipEntry localZipEntry = new ZipEntry(paramString2);
			localZipOutputStream.putNextEntry(localZipEntry);
			return localZipOutputStream;
		} catch (Exception localException2) {
			LogObj.error(localException2);
			throw new XFileException(60017);
		}
	}

	public void makeZipFile(String[] paramArrayOfString, String paramString)
			throws XFileException {
		if ((paramArrayOfString == null) || (paramArrayOfString.length < 1))
			return;
		File localFile1 = new File(paramString);
		if (!(localFile1.exists())) {
			boolean bool = false;
			try {
				bool = localFile1.createNewFile();
			} catch (Exception localException2) {
				LogObj.error(localException2);
			}
			if (!(bool))
				throw new XFileException(60016, localFile1.getName());
		}
		try {
			FileOutputStream localFileOutputStream = new FileOutputStream(
					localFile1);
			ZipOutputStream localZipOutputStream = new ZipOutputStream(
					localFileOutputStream);
			for (int i = 0; i < paramArrayOfString.length; ++i) {
				File localFile2 = new File(paramArrayOfString[i]);
				if (!(localFile2.exists()))
					continue;
				FileInputStream localFileInputStream = new FileInputStream(
						localFile2);
				ZipEntry localZipEntry = new ZipEntry(localFile2.getName());
				localZipOutputStream.putNextEntry(localZipEntry);
				while (true) {
					byte[] arrayOfByte = new byte[10240];
					int j = localFileInputStream.read(arrayOfByte);
					if (j < 1)
						break;
					localZipOutputStream.write(arrayOfByte, 0, j);
				}
				localFileInputStream.close();
				localZipOutputStream.flush();
			}
			localZipOutputStream.close();
		} catch (Exception localException1) {
			LogObj.error(localException1);
			throw new XFileException(60017, localException1.getMessage());
		}
	}

	public long makeZipFile(String paramString1, String paramString2)
			throws XFileException {
		File localFile1 = new File(paramString1);
		if (!(localFile1.exists()))
			throw new XFileException(60002, localFile1.getName());
		File localFile2 = new File(paramString2);
		if (!(localFile2.exists())) {
			boolean bool = false;
			try {
				bool = localFile2.createNewFile();
			} catch (Exception localException2) {
				LogObj.error(localException2);
			}
			if (!(bool))
				throw new XFileException(60016, localFile2.getName());
		}
		try {
			FileInputStream localFileInputStream = new FileInputStream(
					localFile1);
			FileOutputStream localFileOutputStream = new FileOutputStream(
					localFile2);
			ZipOutputStream localZipOutputStream = new ZipOutputStream(
					localFileOutputStream);
			ZipEntry localZipEntry = new ZipEntry(localFile1.getName());
			localZipOutputStream.putNextEntry(localZipEntry);
			while (true) {
				byte[] arrayOfByte = new byte[10240];
				int i = localFileInputStream.read(arrayOfByte);
				if (i < 1)
					break;
				localZipOutputStream.write(arrayOfByte, 0, i);
			}
			localFileInputStream.close();
			localZipOutputStream.flush();
			localZipOutputStream.close();
			return localZipEntry.getCrc();
		} catch (Exception localException1) {
			LogObj.error(localException1);
			throw new XFileException(60017);
		}
	}

	public InputStream unzipFile(InputStream paramInputStream)
			throws XFileException {
		if (paramInputStream != null) {
			ZipInputStream localZipInputStream = new ZipInputStream(
					paramInputStream);
			try {
				ZipEntry localZipEntry = localZipInputStream.getNextEntry();
				if (localZipEntry == null)
					throw new XFileException(60020);
				return localZipInputStream;
			} catch (Exception localException) {
				LogObj.error(localException);
				throw new XFileException(60020);
			}
		}
		return paramInputStream;
	}

	public void unzipFile(InputStream paramInputStream, String paramString)
			throws XFileException {
		if (paramInputStream == null)
			return;
		File localFile = new File(paramString);
		if (!(localFile.exists())) {
			boolean bool = false;
			try {
				bool = localFile.createNewFile();
			} catch (Exception localException1) {
				LogObj.error(localException1);
			}
			if (!(bool))
				throw new XFileException(60016, paramString);
		}
		FileOutputStream localFileOutputStream = null;
		InputStream localInputStream = null;
		try {
			localInputStream = unzipFile(paramInputStream);
			if (localInputStream != null) {
				localFileOutputStream = new FileOutputStream(localFile);
				while (true) {
					byte[] arrayOfByte = new byte[10240];
					int i = localInputStream.read(arrayOfByte);
					if (i < 1)
						break;
					localFileOutputStream.write(arrayOfByte, 0, i);
				}
				localFileOutputStream.flush();
				localFileOutputStream.close();
				localFileOutputStream = null;
				localInputStream.close();
				localInputStream = null;
			}
		} catch (Throwable localThrowable) {
			LogObj.error(localThrowable);
			throw new XFileException(60020);
		} finally {
			try {
				paramInputStream.close();
				if (localFileOutputStream != null)
					localFileOutputStream.close();
				if (localInputStream != null)
					localInputStream.close();
			} catch (Exception localException2) {
			}
		}
	}

	public void unzipFile(String paramString1, String paramString2)
			throws XFileException {
		ZipFile localZipFile = null;
		File localFile1 = new File(paramString1);
		if (!(localFile1.exists()))
			throw new XFileException(60002, localZipFile.getName());
		try {
			localZipFile = new ZipFile(paramString1);
		} catch (Exception localException1) {
			throw new XFileException(60021);
		}
		if ((localZipFile == null) || (localZipFile.size() < 1))
			throw new XFileException(60021);
		File localFile2 = new File(paramString2);
		if (!(localFile2.exists()))
			try {
				if (!(localFile2.createNewFile()))
					throw new XFileException(60016);
			} catch (Exception localException2) {
				LogObj.error(localException2);
				throw new XFileException(60016);
			}
		FileOutputStream localFileOutputStream = null;
		InputStream localInputStream = null;
		try {
			ZipEntry localZipEntry = null;
			Enumeration localEnumeration = localZipFile.entries();
			while (localEnumeration.hasMoreElements()) {
				localZipEntry = (ZipEntry) localEnumeration.nextElement();
				if ((localZipEntry != null) && (!(localZipEntry.isDirectory())))
					break;
			}
			if (localZipEntry != null) {
				localFileOutputStream = new FileOutputStream(localFile2);
				localInputStream = localZipFile.getInputStream(localZipEntry);
				while (true) {
					byte[] arrayOfByte = new byte[10240];
					int i = localInputStream.read(arrayOfByte);
					if (i < 1)
						break;
					localFileOutputStream.write(arrayOfByte, 0, i);
				}
				localInputStream.close();
				localFileOutputStream.flush();
				localFileOutputStream.close();
				localFile2.setLastModified(localZipEntry.getTime());
			}
		} catch (Exception localException3) {
			LogObj.error(localException3);
			throw new XFileException(60020);
		} finally {
			try {
				if (localInputStream != null)
					localInputStream.close();
				if (localFileOutputStream != null)
					localFileOutputStream.close();
			} catch (Exception localException4) {
			}
		}
	}

	public void unzipFileTo(String paramString1, String paramString2)
			throws XFileException {
		ZipFile localZipFile = null;
		File localFile1 = new File(paramString1);
		if (!(localFile1.exists()))
			throw new XFileException(60002, localZipFile.getName());
		try {
			localZipFile = new ZipFile(localFile1);
			if ((localZipFile == null) || (localZipFile.size() < 1))
				throw new XFileException(60021);
			File localFile2 = new File(paramString2);
			if (!(localFile2.exists()))
				throw new XFileException(60022);
			paramString2 = localFile2.getPath();
			Enumeration localEnumeration = localZipFile.entries();
			ZipEntry localZipEntry = null;
			while (localEnumeration.hasMoreElements()) {
				localZipEntry = (ZipEntry) localEnumeration.nextElement();
				if (localZipEntry == null)
					continue;
				String str = localZipEntry.getName();
				if (localZipEntry.isDirectory()) {
					XFileUtil.createDir(paramString2, str);
				} else {
					FileOutputStream localFileOutputStream = null;
					InputStream localInputStream = null;
					try {
						int i = str.indexOf(47);
						if (i > 0) {
							i = str.lastIndexOf(47);
							XFileUtil.createDir(paramString2,
									str.substring(0, i));
							str = str.replace('/', ServerConfig.FILE_SEPARATOR);
						}
						File localFile3 = new File(paramString2
								+ ServerConfig.FILE_SEPARATOR + str);
						if (localFile3.exists())
							throw new XFileException(60020,
									localFile3.getPath());
						localFileOutputStream = new FileOutputStream(localFile3);
						localInputStream = localZipFile
								.getInputStream(localZipEntry);
						while (true) {
							byte[] arrayOfByte = new byte[10240];
							int j = localInputStream.read(arrayOfByte);
							if (j < 1)
								break;
							localFileOutputStream.write(arrayOfByte, 0, j);
						}
						localInputStream.close();
						localFileOutputStream.flush();
						localFileOutputStream.close();
						localFile3.setLastModified(localZipEntry.getTime());
					} catch (XFileException localXFileException2) {
						throw localXFileException2;
					} catch (Exception localException2) {
						LogObj.error(localException2);
						throw new XFileException(60020, str);
					} finally {
						try {
							if (localInputStream != null)
								localInputStream.close();
							if (localFileOutputStream != null)
								localFileOutputStream.close();
						} catch (Exception localException3) {
						}
					}
				}
			}
		} catch (XFileException localXFileException1) {
			throw localXFileException1;
		} catch (Exception localException1) {
			LogObj.error(localException1);
			throw new XFileException(60021);
		} finally {
			try {
				localZipFile.close();
			} catch (Exception localException4) {
			}
		}
	}
}
